/*
Author: Maciej Oczko
Date: 05.11.2011

Singleton code from lecturer materials.
*/

#ifndef SINGLETON_H
#define SINGLETON_H

#include <cassert>

template <typename T>
class Singleton 
{
private:
	Singleton(const Singleton<T> &);
	Singleton& operator=(const Singleton<T> &);
protected:
	static T* ms_Singleton;
public:
	Singleton() 
	{
		assert(!ms_Singleton);
		int offset = (int)(T*)1 - (int)(Singleton<T>*)(T*)1;
		ms_Singleton = (T*)((int)this + offset);
	}
	static T& getInstance()
	{
		assert(ms_Singleton);
		return *ms_Singleton;
	}
	static T* getInstancePtr()
	{
		return ms_Singleton;
	}
	~Singleton()
	{
		ms_Singleton = 0;
	}
};

template<typename T> T* Singleton<T>::ms_Singleton = 0;

#endif